//
//  WHMessageInputCell.m
//  BinFengExpressOwner
//
//  Created by 邓伟浩 on 2018/10/16.
//  Copyright © 2018年 BinFeng. All rights reserved.
//

#import "WHMessageInputCell.h"

@interface WHMessageInputCell ()

@property (nonatomic, strong) UIView *separatorLine;

@end

@implementation WHMessageInputCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = KClearColor;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    [self.contentView addSubview:({
        _tipsImageView = [[UIImageView alloc] init];
        _tipsImageView.contentMode = UIViewContentModeScaleAspectFit;
        _tipsImageView;
    })];
    
    [_tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kRealwidth(32));
        make.width.mas_equalTo(kRealwidth(16));
        make.height.mas_equalTo(kRealwidth(16));
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-kRealwidth(18));
    }];
    
    [self.contentView addSubview:({
        _inputTextField = [[UITextField alloc] init];
        _inputTextField.font = SYSTEMFONT(kRealwidth(17));
        _inputTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _inputTextField;
    })];

    [_inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_tipsImageView.mas_right).offset(kRealwidth(25));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-kRealwidth(32));
        make.height.mas_equalTo(kRealwidth(20));
        make.centerY.mas_equalTo(_tipsImageView.mas_centerY);
    }];
    
    [self.contentView addSubview:({
        _separatorLine = [[UIView alloc] init];
        _separatorLine.backgroundColor = CLineColor;
        _separatorLine;
    })];
    
    [_separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kRealwidth(64));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-kRealwidth(32));
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_inputTextField becomeFirstResponder];
}

- (void)setInputTextPlaceHolder:(NSString *)placeHolderStr {
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeHolderStr attributes:@{NSForegroundColorAttributeName:CFontContentLightGrayColor, NSFontAttributeName:self.inputTextField.font}];
    self.inputTextField.attributedPlaceholder = attrString;
}

#pragma mark - —————— UITextFieldDelegate ——————
- (void)setValidType:(ValideType)validType {
    _validType = validType;
    
    kWeakSelf(self);
    if (self.validType == ValideTypePhoneNumber) {
        self.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.inputTextField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(11).set_regularStr(@"^[0-9]{0,11}+$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (self.validType == ValideTypeUserName) {
        self.inputTextField.keyboardType = UIKeyboardTypeDefault;
        self.inputTextField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(10).set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    } else if (self.validType == ValideTypePassword) {
        self.inputTextField.keyboardType = UIKeyboardTypeASCIICapable;
        self.inputTextField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(12).set_regularStr(@"^[a-zA-Z0-9]{0,12}$").set_textChanged(^(id obj) {
            if (weakself.textChangeBlock) {
                weakself.textChangeBlock([obj valueForKey:@"text"]);
            }
        });
    }
}

- (void)dealloc {
//    DLog(@"=== WHMessageInputCell dealloc ===");
}

@end
